package com.tanhua.server.service;

import com.tanhua.commons.template.HuanXinTemplate;
import com.tanhua.domain.db.Announcement;
import com.tanhua.domain.db.UserInfo;
import com.tanhua.domain.mongo.Comment;
import com.tanhua.domain.mongo.Friend;
import com.tanhua.domain.vo.AnnouncementVO;
import com.tanhua.domain.vo.ContactVO;
import com.tanhua.domain.vo.MessageCommentVO;
import com.tanhua.domain.vo.PageResult;
import com.tanhua.dubbo.api.AnnouncementApi;
import com.tanhua.dubbo.api.CommentApi;
import com.tanhua.dubbo.api.FriendApi;
import com.tanhua.dubbo.api.UserInfoApi;
import com.tanhua.server.interceptor.UserHolder;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.Reference;
import org.joda.time.DateTime;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Slf4j
@Service
public class MessageService {
    @Reference
    private FriendApi friendApi;
    @Reference
    private UserInfoApi userInfoApi;
    @Reference
    private CommentApi commentApi;
    @Autowired
    private HuanXinTemplate huanXinTemplate;
    @Reference
    private AnnouncementApi announcementApi;
    /**
     * 添加好友关系
     * @param targetUserId  目标用户
     * @return
     */
    public ResponseEntity addContact(Long targetUserId) {
        Long userId = UserHolder.getUser().getId();
        //1. 将好友关系保存到mongodb
        friendApi.addContact(userId, targetUserId);
        //2. 将好友关系注册到环信
        huanXinTemplate.addContact(userId.toString(), targetUserId.toString());
        log.info("注册好友关系成功");
        return ResponseEntity.ok(null);
    }

    /**
     * 联系人列表
     */
    public ResponseEntity contactsList(Integer page, Integer pagesize, String keyword) {
        Long userId = UserHolder.getUser().getId();
        PageResult pageResult = friendApi.findFriendList(page, pagesize, userId);
        List<Friend> items = pageResult.getItems();

        //转化vo
        List<ContactVO> vos = new ArrayList<>();
        for (Friend friend : items) {
            ContactVO vo = new ContactVO();
            //封装好友信息
            UserInfo userInfo = userInfoApi.findById(friend.getFriendId());
            BeanUtils.copyProperties(userInfo, vo);
            vo.setUserId(friend.getFriendId().toString());
            //封装当前用户id到vo
            vo.setId(userId);
            vos.add(vo);
        }
        pageResult.setItems(vos);
        return ResponseEntity.ok(pageResult);
    }

    /**
     * 评论列表
     * @param commentType 评论类型 1表示点赞 2表示文字评论 3表示喜欢
     * @param pubType   评论对象类型  1表示动态 2表示视频 3表示文字评论
     * @return
     */
    public ResponseEntity findCommentListByType(Integer page, Integer pagesize, Integer commentType, Integer pubType) {
        Long userId = UserHolder.getUser().getId();
        PageResult pageResult = commentApi.findCommentListByType(page, pagesize, commentType, pubType,  userId);

        //转换vo
        List<Comment> commetUserList = pageResult.getItems();
        List<MessageCommentVO> vos = new ArrayList<>();
        for (Comment comment : commetUserList) {
            MessageCommentVO vo = new MessageCommentVO();
            UserInfo userInfo = userInfoApi.findById(comment.getUserId());
            BeanUtils.copyProperties(userInfo,vo);
            vo.setId(userInfo.getId().toString());
            vo.setCreateDate(new DateTime(comment.getCreated()).toString("yyyy年MM月dd日 HH:mm"));
            vos.add(vo);
        }
        pageResult.setItems(vos);
        return ResponseEntity.ok(pageResult);
    }
    public ResponseEntity findAnnouncementList(int page, int pagesize) {
        //查询公告信息
        //总数量
        //pagesize
        //page
        //items 公告内容
        //id
        //title 标题
        //description 公告内容
        //createDate 发布时间
        PageResult pageResult = announcementApi.findAnnouncementList( page,pagesize);
        //把评论信息封装到vo对象里
        //2. 把每个公告announcement对象转换成VO对象
        List<Announcement> items = pageResult.getItems();
        List<AnnouncementVO> voList = new ArrayList<>();
        for (Announcement item : items) {
            AnnouncementVO vo = new AnnouncementVO();
            BeanUtils.copyProperties(item,vo);
            Date created = item.getCreated();
            String format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(created);
            vo.setCreateDate(format);
            voList.add(vo);
        }
        pageResult.setItems(voList);
        return ResponseEntity.ok(pageResult);


    }


}
